﻿/// <reference path="../core/jquery-1.4.2.min-vsdoc.js"/>
/// <reference path="../config/sw-config-default.js"/>
/// <reference path="../core/sw-prototype.js"/>
/// <reference path="../core/sw.js"/>
/// <reference path="../core/sw-control-core.js"/>

/***************************************************************************
jQuery menu
NOTE: Created for SmartUI. 
Copyright 2010, Roy zhang
Features：
1.实现Menu，Accordion功能
2.支持多级Menu、html转换、数据绑定、自定义显示模板
3.支持menu的选择定位
4.
Update Note：

Needs：
****************************************************************************/

(function ($) {
    $.fn.menu = function (datas, options) {
        /// <summary>pager</summary>
        /// <param name="options" type="Array">配置项</param>
        /// <param name="datas" type="Json">数据源</param>               
        options = $.extend({
            container: iconfig.menu.menu.container
            , item: iconfig.menu.menu.item
        }, options);

        this.each(function () {
            var menu = $(this), id = menu.attr('id'), timeout = false;
            if (datas)
                bindMenu(id, menu, datas, options);
            bulidMenu(id, menu.children('ul').children('li'), null, timeout);
        });
        initSelected(this);
        ///#endregion

    };

    $.fn.accordion = function (datas, options) {
        /// <summary>pager</summary>
        /// <param name="options" type="Array">配置项</param>
        /// <param name="datas" type="Json">数据源</param>          
        options = $.extend({
            container: iconfig.menu.accordion.container
            , item: iconfig.menu.accordion.item
            , menu: {
                container: iconfig.menu.menu.container
            , item: iconfig.menu.menu.item
            }
        }, options);

        this.each(function () {
            var accordion = $(this), id = accordion.attr('id'), timeout = false;
            if (datas && datas.length > 0) {
                var container = $(options.container).appendTo(accordion);
                ictrcore.teml(container, options.item, datas)
                .each(function (i) {
                    var dd = $('<dd />').insertAfter(this);
                    var data = datas[i];
                    if (data && data.rows)
                        bindMenu(id, dd, data.rows, options.menu);
                    bulidMenu(id, dd.children('ul').children('li'), true, timeout);
                });
            }
            accordion.find('dt > a').click(function () {
                var link = $(this);
                var item = link.parent().toggleClass("selected").next().slideToggle("slow");
            });

        })
        initSelected(this, true);
    }

    ///#region 公共设置方法
    function bindMenu(menuid, menu, datas, options) {
        if (datas && datas.length > 0) {
            var container = $(options.container).appendTo(menu);
            ictrcore.teml(container, options.item, datas).each(function (i) {
                var data = datas[i];
                if (data && data.rows)
                    bindMenu(menuid, $(this), data.rows, options);
            });
        }
    }
    function bulidMenu(menuid, items, isHorizontal, timeout) {
        if (items.length > 0) {
            items.each(function () {
                var item = $(this), link = item.children('a'), child = item.children('ul');
                item.click(function () {
                    $('#' + menuid + ' li.selected').removeClass('selected');
                    $(this).addClass('selected');
                });
                if (child.length > 0) {
                    item.addClass('dir');
                    item.hover(function () {
                        show($(this), isHorizontal, timeout);
                    }, function () {
                        close($(this), timeout);
                    });
                    bulidMenu(menuid, child.children('li'), true, timeout);
                }
            });
        }
    }
    function show(menu, isHorizontal, timeout) {
        if (timeout) { clearTimeout(timeout); }
        timeout = setTimeout(function () {
            menu.stop(true, true).show('fast');
            showChildMenu(menu, isHorizontal);
        }, 100);
    }
    function close(menu, timeout) {
        if (timeout) { clearTimeout(timeout); }
        timeout = setTimeout(function () { menu.children('ul').hide('fast'); }, 100);
    }

    function setMenuChild(ctr, child, isHorizontal) {
        /// <summary>设置选择控件弹出层</summary>
        /// <param name="ctr" type="JQDom">控件</param>
        /// <param name="selector" type="JQDomorDomStr">选择器对象</param>
        /// <param name="css" type="Json">样式对象</param>
        /// <returns type="JQDom" />
        var position = ctr.position();
        //        var scroll = $.getWinScroll();
        //        if (isHorizontal)
        //            position.left += ctr.outerWidth() + scroll.left;
        //        else
        //            position.top += ctr.outerHeight() + scroll.top;
        if (isHorizontal)
            position.left += ctr.outerWidth();
        else
            position.top += ctr.outerHeight();

        return child.css({ "z-index": iconfig.menu.css.zindex, 'left': position.left, 'top': position.top
        }).mouseleave(function () {
            $(this).hide('fast');
        });
    }
    function showChildMenu(menu, isHorizontal) {
        var child = menu.children('ul');
        setMenuChild(menu, child, isHorizontal);
        child.stop(true, true).show('fast');
    }

    //根据Url参数设置选择的menu
    function initSelected(menu, isAccordion) {
        var isSelected = false, allpath = getAllPathUI();
        if (allpath) {
            var ui = allpath[0];
            for (var i = 0; allpath[i]; i++) {
                var selected = menu.find("a[ui='" + allpath[i] + "']");
                if (selected.length > 0) {
                    isSelected = true;
                    selected.parent().addClass('selected');
                    if (isAccordion)
                        selected.parents('dd').show().prev().addClass('selected');
                }
            }
//            var selected = menu.find("a[ui='" + ui + "']");
//            if (selected.length > 0) {
//                isSelected = true;
//                selected.parents('li').addClass('selected');
//                if (isAccordion)
//                    selected.parents('dd').show().prev().addClass('selected');
//            }
        } else //默认时，找到第一个匹配标签
            menu.find("a:eq(0)").parents().addClass('selected');
    }

    ///#region 公共方法
    ///#endregion

})(jQuery);